package project.model;

import java.io.Serializable;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import models.util.MySqlDataAccessHelper;
import models.util.UtilInitLang;




public class PrTestProject  implements Serializable {

	private static final long serialVersionUID = 6477538392979499106L;


	private String idTest;
	private String idProject;
	private String idComposition;
	private String title;
	private String titleview;
	private String text;
	private Date time;
	private int status;
	private int delete;
	private String displayDate;
	private String nameEmployee;
	private String strStatus;
//	DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
	private  String colorText;
	private PrComment lastComment;
	private String db="admin";
	private String idlang="EN";
	
	private String employeeName;
	private String idmanager;
	
	private ArrayList<PrComment> listCom = new ArrayList<PrComment>();
	private String idCom=null;
	
//	private static int seecolor;
//	
//	
//	public int getSeecolor() {
//		seecolor=R.color.errXanh;
//		int index = title.indexOf("-%$#@!-");
//		if(index > 0){
//			colorText = title.substring(index + 7, title.length());
//			if(colorText.equals("#B023AB")){
//				seecolor=R.color.errTim;
//			}else if(colorText.equals("#B02323")){
//				seecolor=R.color.errDo;
//			}else if(colorText.equals("#3DA850")){
//				seecolor=R.color.errXanh;
//			}
//		}
////		System.out.println("mau: "+R.color.errTim);
//		return seecolor;
//	}
//
//	public void setSeecolor(int seecolor) {
//		PrTestProject.seecolor = seecolor;
//	}

	public String getIdCom(String dbc) {
		String s=null;
		if(dbc!=null){
			MySqlDataAccessHelper mysql =new MySqlDataAccessHelper();
			try{
				mysql.open(dbc);
				String sql="select pr_comment.IDComment from pr_comment where pr_comment.IDTest='"+this.idTest+"'";
				ResultSet rs= mysql.executeQuery(sql);
				
				while(rs.next()){
					
					if(s.equals(null)){
						s=rs.getString("IDComment");
					}else{
						s+=","+rs.getString("IDComment");
					}
				}
				
				if(s.equals(null)==false){
					idCom=s;
				}
				
				rs.close();
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				mysql.close();
			}
		}
		return idCom;
	}

	public void setIdCom(String idCom) {
		this.idCom = idCom;
	}

	public ArrayList<PrComment> getListCom(String dbc) {
		if(dbc!=null){
			MySqlDataAccessHelper mysql =new MySqlDataAccessHelper();
			try{
				mysql.open(dbc);
				String sql="select pr_comment.IDComment,pr_comment.IDTest " +
						"from pr_comment " +
						"where pr_comment.IDTest='"+this.idTest+"'";
				ResultSet rs= mysql.executeQuery(sql);
				PrComment pc ;
				while(rs.next()){
					pc = new PrComment();
					pc.setDb(dbc);
					pc.setIdComment(rs.getString("IDComment"));
					pc.setIdTestProject(rs.getString("IDTest"));
					listCom.add(pc);
				}
				rs.close();
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				mysql.close();
			}
		}
		return listCom;
	}

	public void setListCom(ArrayList<PrComment> listCom) {
		this.listCom = listCom;
	}

	public String getIdlang() {
		return idlang;
	}

	public void setIdlang(String idlang) {
		this.idlang = idlang;
	}

	public String getDb() {
		return db;
	}

	public void setDb(String db) {
		this.db = db;
	}

	public PrTestProject(){
		
	}
	
	public PrTestProject(String idTest, String idProject, String idComposition, String title, String text, Date time, int status) {
		this.idTest = idTest;
		this.idProject = idProject;
		this.idComposition = idComposition;
		this.title = title;
		this.text = text;
		this.time = time;
		this.status = status;
	}
	
	public PrTestProject(String idTest, String idProject, String idComposition, String title, String text, Date time, int status, int delete, String nameEmployee) {
		this.idTest = idTest;
		this.idProject = idProject;
		this.idComposition = idComposition;
		int index = title.indexOf("-%$#@!-");
		if(index > 0){
			this.title = title.substring(0,index);
			this.colorText = title.substring(index + 7, title.length());
		}else{
			this.title = title;
			this.colorText = "";
		}		
		this.text = text;
		this.time = time;
		this.status = status;
		this.delete = delete;
		this.nameEmployee = nameEmployee;
	}
	
	public PrTestProject(String idTest, String idProject, String title, String text, Date time, String nameEmployee) {
		this.idTest = idTest;
		this.idProject = idProject;
//		this.idComposition = idComposition;
		int index = title.indexOf("-%$#@!-");
		if(index > 0){
			this.title = title.substring(0,index);
			this.colorText = title.substring(index + 7, title.length());
		}else{
			this.title = title;
			this.colorText = "";
		}		
		this.text = text;
		this.time = time;
//		this.status = status;
//		this.delete = delete;
		this.nameEmployee = nameEmployee;
	}
	
	public String getIdTest() {
		return idTest;
	}

	public void setIdTest(String idTest) {
		this.idTest = idTest;
	}

	public String getIdProject() {
		return idProject;
	}

	public void setIdProject(String idProject) {
		this.idProject = idProject;
	}

	public String getIdComposition() {
		return idComposition;
	}

	public void setIdComposition(String idComposition) {
		this.idComposition = idComposition;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	public void setDisplayDate(String displayDate) {
		this.displayDate = displayDate;
	}

	public String getDisplayDate() {
//		if(time != null){
//			displayDate = format.format(time);
//		}
		return displayDate;
	}

	public void setNameEmployee(String nameEmployee) {
		this.nameEmployee = nameEmployee;
	}

	public String getNameEmployee() {
		
		if(this.getDb()!=null){
			MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
			try{
				helper.open(this.db);
				String sql="select employee.FullName " +
						"from employee " +
						"where employee.IDEmployee='"+this.idComposition+"'";
				ResultSet rs=helper.executeQuery(sql);
				if(rs.next()){
					nameEmployee=rs.getString("FullName");
				}
				rs.close();
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				helper.close();
			}
		}		
		return nameEmployee;
	}

	public void setStrStatus(String strStatus) {
		this.strStatus = strStatus;
	}

	@SuppressWarnings("static-access")
	public String getStrStatus(String chooselang) {
		UtilInitLang lang = new UtilInitLang();
		lang.setDb(chooselang);
		if(status == 0){
			strStatus = lang.getStatus_error2();
			
		} else {
			strStatus = lang.getStatus_error1();
		}
		if(strStatus==null || strStatus.equals("")){
			strStatus="hix";
		}
		
		return strStatus;
	}
	
	public static void main(String[] args) {
		
	}

	public void setDelete(int delete) {
		this.delete = delete;
	}

	public int getDelete() {
		return delete;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		
		return title;
	}

	
	
	public String getTitleview() {
		titleview=title;
		int index = titleview.indexOf("-%$#@!-");
		if(index > 0){
			titleview = title.substring(0,index);
		}
		return titleview;
	}

	public void setTitleview(String titleview) {
		this.titleview = titleview;
	}

	public void setColorText(String colorText) {
		
		this.colorText = colorText;
	}

	public String getColorText() {
		int index = title.indexOf("-%$#@!-");
		
		if(index > 0){
			colorText = title.substring(index + 7, title.length());
		}else{
			colorText = "#424242";
		}
		return colorText;
	}

	public void setLastComment(PrComment lastComment) {
		this.lastComment = lastComment;
	}

	public PrComment getLastComment() {
//		CommentDAO cdao = new CommentDAO();
//		try {
//			lastComment = cdao.getLastCommentWithIDTest(idTest);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}y
		return lastComment;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
//		String status=null;
//		if(this.getStatus()==0)
//			status="No fix";
//		else 
//			status="Fix complete";
//		return "+ "+this.title+" ("+this.nameEmployee+"-"+this.strStatus+"-"+this.displayDate+")";
//		return "+ "+this.title;
		return "+ "+this.title+" ("+this.displayDate+")";
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public String getIdmanager() {
		return idmanager;
	}

	public void setIdmanager(String idmanager) {
		this.idmanager = idmanager;
	}

}
